package com.fr.report.core;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.antlr.ANTLRException;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.file.ConfigManager;
import com.fr.chart.ChartCollection;
import com.fr.data.Dictionary;
import com.fr.data.TableData;
import com.fr.data.impl.NameTableData;
import com.fr.data.impl.TableDataDictionary;
import com.fr.report.CellElement;
import com.fr.report.FloatElement;
import com.fr.report.ReportParameterAttr;
import com.fr.report.TemplateReport;
import com.fr.report.TemplateWorkBook;
import com.fr.report.cellElement.core.DSColumn;
import com.fr.report.parameter.CustomParameterProcessor;
import com.fr.report.parameter.Parameter;
import com.fr.report.parameter.ParameterProcessor;
import com.fr.report.script.Calculator;
import com.fr.report.script.DataSetFunctionParameterMapNameSpace;
import com.fr.report.script.ParameterMapNameSpace;
import com.fr.report.script.core.parser.Expression;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParameterHelper
{
  private static final Pattern deprecatedParaPattern = Pattern.compile("\\[\\?[^\\]\\?]*\\?\\]");

  public static Parameter[] analyze4Parameters(String paramString)
  {
    Parameter[] arrayOfParameter = null;
    if (deprecatedParaPattern.matcher(paramString).find())
    {
      SegmentHandler.ParameterCollector localParameterCollector = new SegmentHandler.ParameterCollector();
      analyzeParametersFromQuery(paramString, localParameterCollector);
      arrayOfParameter = localParameterCollector.getParameters();
    }
    else if (TemplateUtils.ParameterPattern.matcher(paramString).find())
    {
      arrayOfParameter = parametersFromQuery(paramString);
    }
    return ((arrayOfParameter == null) ? new Parameter[0] : arrayOfParameter);
  }

  public static String analyze4Templatee(String paramString, Parameter[] paramArrayOfParameter)
  {
    if ((paramArrayOfParameter == null) || (paramArrayOfParameter.length == 0))
      return paramString;
    Calculator localCalculator = Calculator.createCalculator();
    localCalculator.pushNameSpace(ParameterMapNameSpace.create(paramArrayOfParameter));
    return localCalculator.renderTpl(paramString);
  }

  public static String analyzeCurrentContextTableData4Templatee(String paramString, Parameter[] paramArrayOfParameter)
  {
    Calculator localCalculator = Calculator.createCalculator();
    localCalculator.pushNameSpace(DataSetFunctionParameterMapNameSpace.creat(paramArrayOfParameter));
    return localCalculator.renderTpl(paramString);
  }

  public static void analyzeParametersFromQuery(String paramString, SegmentHandler paramSegmentHandler)
  {
    Matcher localMatcher = deprecatedParaPattern.matcher(paramString);
    int i = 0;
    int j = 0;
    while (localMatcher.find())
    {
      j = localMatcher.start();
      str1 = paramString.substring(i, j);
      if (StringUtils.isNotEmpty(str1))
        paramSegmentHandler.stringSegment(str1);
      i = localMatcher.end();
      String str2 = localMatcher.group();
      String str3 = str2.replaceAll("[\\[\\?\\?\\]]", "");
      String[] arrayOfString = str3.split("\\|");
      String str4 = arrayOfString[0].replaceAll("[\\r\\n, \\n]*", "");
      paramSegmentHandler.parameterSegment(str4, (arrayOfString.length > 1) ? arrayOfString[1] : null);
    }
    String str1 = paramString.substring(i);
    if (StringUtils.isNotBlank(str1))
      paramSegmentHandler.stringSegment(str1);
  }

  private static Parameter[] parametersFromQuery(String paramString)
  {
    Object localObject2;
    Expression localExpression;
    Matcher localMatcher = Calculator.ParameterPattern.matcher(paramString);
    ArrayList localArrayList = new ArrayList();
    HashSet localHashSet = new HashSet();
    do
    {
      while (true)
      {
        if (!(localMatcher.find()))
          break label137;
        localObject1 = localMatcher.group();
        localObject2 = ((String)localObject1).substring(2, ((String)localObject1).length() - 1);
        if (!(StringUtils.isBlank((String)localObject2)))
          break;
      }
      localExpression = null;
      Calculator localCalculator = Calculator.createCalculator();
      try
      {
        localExpression = localCalculator.parse((String)localObject2);
      }
      catch (ANTLRException localANTLRException)
      {
        localANTLRException.printStackTrace();
      }
    }
    while (localExpression.parserParameter() == null);
    int i = 0;
    int j = localExpression.parserParameter().length;
    while (true)
    {
      if (i >= j);
      localHashSet.add(localExpression.parserParameter()[i]);
      ++i;
    }
    label137: Object localObject1 = localHashSet.iterator();
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = new Parameter((String)((Iterator)localObject1).next());
      localArrayList.add(localObject2);
    }
    return ((Parameter)(Parameter)(Parameter[])localArrayList.toArray(new Parameter[localArrayList.size()]));
  }

  public static void addGlobal_ParameterToList(List paramList)
  {
    ConfigManager localConfigManager = FRContext.getConfigManager();
    Parameter[] arrayOfParameter = localConfigManager.getGlobal_Parameters();
    if (arrayOfParameter != null)
      paramList.addAll(Arrays.asList(arrayOfParameter));
  }

  public static void addReportParameterAttrToList(List paramList, ReportParameterAttr paramReportParameterAttr, boolean paramBoolean)
  {
    Parameter[] arrayOfParameter;
    int i;
    if (paramReportParameterAttr == null)
      return;
    if (paramBoolean)
    {
      ParameterProcessor localParameterProcessor = paramReportParameterAttr.getParameterProcessor();
      if (localParameterProcessor instanceof CustomParameterProcessor)
      {
        arrayOfParameter = ((CustomParameterProcessor)localParameterProcessor).getParameters();
        if ((arrayOfParameter != null) && (arrayOfParameter.length > 0))
          for (i = 0; i < arrayOfParameter.length; ++i)
            if (arrayOfParameter[i] != null)
              paramList.add(arrayOfParameter[i]);
      }
    }
  }

  public static void addReportTableDataParameterToList(List paramList, TemplateReport paramTemplateReport, boolean paramBoolean)
    throws Exception
  {
    Object localObject1;
    Object localObject2;
    if (paramTemplateReport == null)
      return;
    HashMap localHashMap = new HashMap();
    if (paramTemplateReport.getTemplateWorkBook() != null)
    {
      localIterator = paramTemplateReport.getTemplateWorkBook().getTableDataNameIterator();
      while (localIterator.hasNext())
      {
        localObject1 = (String)localIterator.next();
        localHashMap.put(localObject1, paramTemplateReport.getTemplateWorkBook().getTableData((String)localObject1));
      }
    }
    if (paramBoolean)
    {
      localIterator = paramTemplateReport.cellIterator();
      while (localIterator.hasNext())
      {
        localObject1 = (CellElement)localIterator.next();
        localObject2 = ((CellElement)localObject1).getValue();
        collectValueTableData(localObject2, localHashMap);
      }
      localObject1 = paramTemplateReport.floatIterator();
      while (((Iterator)localObject1).hasNext())
      {
        localObject2 = (FloatElement)((Iterator)localObject1).next();
        Object localObject3 = ((FloatElement)localObject2).getValue();
        collectValueTableData(localObject3, localHashMap);
      }
    }
    Iterator localIterator = localHashMap.keySet().iterator();
    while (localIterator.hasNext())
    {
      localObject1 = (String)localIterator.next();
      localObject2 = (TableData)localHashMap.get(localObject1);
      addParametersToParameterList(paramList, ((TableData)localObject2).getParameters());
    }
  }

  public static void refreshParameterProcessor(TemplateReport paramTemplateReport)
  {
    Map localMap;
    ArrayList localArrayList2;
    int i;
    if (paramTemplateReport == null)
      return;
    ReportParameterAttr localReportParameterAttr = (paramTemplateReport.getTemplateWorkBook() == null) ? null : paramTemplateReport.getTemplateWorkBook().getReportParameterAttr();
    if (localReportParameterAttr == null)
      return;
    if (localReportParameterAttr.getParameterProcessor() instanceof CustomParameterProcessor)
    {
      ArrayList localArrayList1 = new ArrayList();
      addReportParameterAttrToList(localArrayList1, localReportParameterAttr, false);
      try
      {
        addReportTableDataParameterToList(localArrayList1, paramTemplateReport, true);
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
      addGlobal_ParameterToList(localArrayList1);
      CustomParameterProcessor localCustomParameterProcessor = (CustomParameterProcessor)localReportParameterAttr.getParameterProcessor();
      localMap = localCustomParameterProcessor.getUnitProcessorMap();
      Iterator localIterator = localMap.entrySet().iterator();
      localArrayList2 = new ArrayList();
      while (true)
      {
        String str;
        int j;
        int k;
        do
        {
          if (!(localIterator.hasNext()))
            break label214;
          Map.Entry localEntry = (Map.Entry)localIterator.next();
          str = (String)localEntry.getKey();
          j = 0;
          for (k = 0; k < localArrayList1.size(); ++k)
            if (((Parameter)localArrayList1.get(k)).getName().equals(str))
              j = 1;
        }
        while (j != 0);
        localArrayList2.add(str);
      }
      for (i = 0; i < localArrayList2.size(); ++i)
        label214: localMap.remove(localArrayList2.get(i));
    }
  }

  public static void collectValueTableData(Object paramObject, Map paramMap)
  {
    Object localObject1;
    String str;
    Object localObject2;
    if (paramObject instanceof DSColumn)
    {
      localObject1 = (DSColumn)paramObject;
      str = ((DSColumn)localObject1).getDSName();
      localObject2 = (TableData)paramMap.get(str);
      if (localObject2 == null)
        paramMap.put(str, new NameTableData(str));
    }
    else if (paramObject instanceof ChartCollection)
    {
      localObject1 = (ChartCollection)paramObject;
      str = null;
      if ((str != null) && (str instanceof NameTableData))
      {
        localObject2 = ((NameTableData)str).getName();
        if (paramMap.get(localObject2) == null)
          paramMap.put(localObject2, str);
      }
    }
  }

  public static void addDictParameterToList(Dictionary paramDictionary, Map paramMap, List paramList)
    throws Exception
  {
    if (paramDictionary == null)
      return;
    if (paramDictionary instanceof TableDataDictionary)
    {
      Object localObject;
      TableDataDictionary localTableDataDictionary = (TableDataDictionary)paramDictionary;
      TableData localTableData1 = localTableDataDictionary.getTableData();
      if (localTableData1 instanceof NameTableData)
      {
        localObject = ((NameTableData)localTableData1).getName();
        TableData localTableData2 = (TableData)paramMap.get(localObject);
        if (localTableData2 == null)
          paramMap.put(localObject, localTableData1);
        else
          localTableDataDictionary.setTableData(localTableData2);
      }
      else if (localTableData1 != null)
      {
        localObject = localTableData1.getParameters();
        if (localObject != null)
          paramList.addAll(Arrays.asList(localObject));
      }
    }
  }

  public static void addParametersToParameterList(List paramList, Parameter[] paramArrayOfParameter)
  {
    if (paramArrayOfParameter == null)
      return;
    for (int i = 0; i < paramArrayOfParameter.length; ++i)
    {
      Parameter localParameter = paramArrayOfParameter[i];
      if (localParameter == null)
        break label32:
      paramList.add(localParameter);
    }
    label32:
  }

  public static Parameter[] cloneParameters(Parameter[] paramArrayOfParameter)
    throws CloneNotSupportedException
  {
    if (paramArrayOfParameter == null)
      return null;
    Parameter[] arrayOfParameter = new Parameter[paramArrayOfParameter.length];
    for (int i = 0; i < paramArrayOfParameter.length; ++i)
    {
      Parameter localParameter = paramArrayOfParameter[i];
      if (localParameter == null)
        arrayOfParameter[i] = new Parameter();
      else
        arrayOfParameter[i] = ((Parameter)localParameter.clone());
    }
    return arrayOfParameter;
  }
}